﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.WizardFramework;
using System.ComponentModel.Design;
using Microsoft.Practices.Modeling.Dsl.Integration.Helpers;
using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Shell;
using EastBancTech.InstallerDefinition;
using FileActionHelpers = EBT.Deployment.SoftwareFactory.FileActions.Helpers;

namespace EBT.Deployment.SoftwareFactory.CustomWizardPages
{
    /// <summary>
    /// Example of a class that is a custom wizard page
    /// </summary>
    public partial class FileActionWizardPage : CustomWizardPage
    {
        
        protected InstallerPackageModel _package;
        protected FileActionHelpers::FileActionHelper _helper;

        public FileActionWizardPage(WizardForm parent)
            : base(parent)
        {
            // This call is required by the Windows Form Designer.
            InitializeComponent();

        }

        Store _store;
        [RecipeArgument]
        public Store WixPackageStore
        {
            set
            {
                _store = value;

                DomainClassInfo classInfo = _store.DomainDataDirectory.GetDomainClass(typeof(FileAction));
                _package = DomainModelHelper.GetElement<InstallerPackageModel>(_store);

                this.cmbFileActionType.Items.Clear();
                foreach (DomainClassInfo descendantClassInfo in classInfo.AllDescendants)
                {
                    this.cmbFileActionType.Items.Add(descendantClassInfo);
                }
                if (this.cmbFileActionType.Items.Count != 0)
                {
                    this.cmbFileActionType.SelectedIndex = 0;
                }


                
                cmbFileSet.Items.Clear();
                foreach (FileSet fileSet in _package.Definitions.FileSets)
                {
                    this.cmbFileSet.Items.Add(new FileActionHelpers::NamedElement<FileSet>(fileSet.Name, fileSet));
                }

            }
        }
       

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            IDictionaryService dictionaryService = GetService(typeof(IDictionaryService)) as IDictionaryService;
            dictionaryService.SetValue("Name", this.txtName.Text);
        }


        private void cmbFileActionType_SelectedIndexChanged(object sender, EventArgs e)
        {
            IDictionaryService dictionaryService = GetService(typeof(IDictionaryService)) as IDictionaryService;
            dictionaryService.SetValue("Type", this.cmbFileActionType.SelectedItem);
            dictionaryService.SetValue("Values", new ModelElement[0]);

            
            clbValues.Items.Clear();           

            if (_store != null)
            {
                _helper = FileActionHelpers::FileActionHelper.CreateInstance((DomainClassInfo)this.cmbFileActionType.SelectedItem);
                System.Collections.IEnumerable values = _helper.GetValues(_package);
                foreach (ModelElement value in values)
                {
                    FileActionHelpers::NamedElement<ModelElement> namedValue =
                        new FileActionHelpers::NamedElement<ModelElement>(_helper.GetElementName(value), value);
                    clbValues.Items.Add(namedValue);                    
                }
            }

        }


        private void clbValues_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            IDictionaryService dictionaryService = GetService(typeof(IDictionaryService)) as IDictionaryService;
            List<ModelElement> list = new List<ModelElement>();
            FileActionHelpers::NamedElement<ModelElement> newElement =
                (FileActionHelpers::NamedElement<ModelElement>)clbValues.Items[e.Index];
            foreach (FileActionHelpers::NamedElement<ModelElement> element in this.clbValues.CheckedItems)
            {
                
                if ((newElement.Name !=element.Name)||(e.NewValue == CheckState.Checked))
                {
                    list.Add(element.Element);
                }
            }
            if (e.NewValue == CheckState.Checked)
            {
                list.Add(newElement.Element);
            }
            dictionaryService.SetValue("Values", list.ToArray());
        }

        private void cmbFileSet_SelectedIndexChanged(object sender, EventArgs e)
        {
            IDictionaryService dictionaryService = GetService(typeof(IDictionaryService)) as IDictionaryService;
            if (this.cmbFileSet.SelectedItem == null)
                dictionaryService.SetValue("FileSet", null);
            else
            {
                FileActionHelpers::NamedElement<FileSet> element =
                    (FileActionHelpers::NamedElement<FileSet>)cmbFileSet.SelectedItem;
                dictionaryService.SetValue("FileSet", element.Element);
            }
        } 
    }
}
